package com.itheima.service.imp;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.constant.MessageConstant;
import com.itheima.dao.MemberDao_lzl;
import com.itheima.dao.OrderDao_lzl;
import com.itheima.dao.OrderSettingDao_lzl;
import com.itheima.entity.PageResult;
import com.itheima.entity.Result;
import com.itheima.pojo.Member;
import com.itheima.pojo.Order;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderService_lzl;
import com.itheima.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = OrderService_lzl.class)
@Transactional
public class OrderServiceImpl_lzl implements OrderService_lzl {
    @Autowired
    private OrderDao_lzl orderDao_lzl;
    @Autowired
    private OrderSettingDao_lzl orderSettingDao;
    @Autowired
    private MemberDao_lzl memberDao_lzl;
    /*
     * 业务逻辑：
     *   1.先判断当前日期是否可预约
     *   2.在判断当前日期预约人数是否已满
     *   3.判断当前用户是否为会员，是的话判断是否已经预约了该套餐，不是的话注册会员，进行预约
     *   4.如果已经预约了该套餐，则返回已经预约，否则进行预约
     * */
    @Override
    public Result submitOrder(Map map) {
        String telephone = (String) map.get("telephone");
        Date orderDate = null;
        try {
            orderDate = DateUtils.parseString2Date((String) map.get("orderDate"));
        } catch (Exception e) {
            e.printStackTrace();
        }
        OrderSetting orderSetting = orderSettingDao.findOrderSettingByOrderDate(orderDate);
        if(orderSetting!=null){
            //当前日期可以预约
            int number = orderSetting.getNumber();
            int reservations = orderSetting.getReservations();
            if(number>reservations){
                //预约未满
                Member member = memberDao_lzl.findMembertBytelephone(telephone);
                if(member==null){
                    //该用户不是会员
                    //注册会员
                    Member member1 = new Member();
                    member1.setName((String) map.get("name"));
                    member1.setSex((String) map.get("sex"));
                    member1.setIdCard((String) map.get("idCard"));
                    member1.setPhoneNumber(telephone);
                    member1.setRegTime(new Date());
                    memberDao_lzl.addMember(member1);
                    //进行预约
                    Order order = new Order();
                    order.setMemberId(member1.getId());
                    order.setOrderDate(orderDate);
                    order.setOrderType(Order.ORDERTYPE_WEIXIN);
                    order.setOrderStatus(Order.ORDERSTATUS_NO);
                    order.setSetmealId(Integer.parseInt((String) map.get("setmealId")));
                    orderDao_lzl.addOrder(order);
                    //已预约人数加1
                    orderSetting.setReservations(reservations+1);
                    orderSettingDao.updateByOrderDate(orderSetting);
                    //返回预约成功，返回预约id
                    return new Result(true,MessageConstant.ORDER_SUCCESS,order.getId());
                }else {
                    //该用户是会员
                    //在判断该会员当前是否已预约该套餐
                    Integer memberId = member.getId();//会员id
                    Integer setmealId =Integer.parseInt((String) map.get("setmealId"));//套餐id
                    Order order = new Order();
                    order.setSetmealId(setmealId);
                    order.setMemberId(memberId);
                    order.setOrderDate(orderDate);
                    order.setOrderType(Order.ORDERTYPE_WEIXIN);
                    order.setOrderStatus(Order.ORDERSTATUS_NO);
                    List<Order> orderList = orderDao_lzl.findOrderByMemberIdAndSetmealId(order);
                    if(orderList!=null&&orderList.size()>0){
                        return new Result(false, MessageConstant.HAS_ORDERED);
                    }else {
                        orderDao_lzl.addOrder(order);
                        //已预约人数加1
                        orderSetting.setReservations(reservations+1);
                        orderSettingDao.updateByOrderDate(orderSetting);
                        //返回预约成功，返回预约id
                        return new Result(true,MessageConstant.ORDER_SUCCESS,order.getId());
                    }
                }

            }else {
                //预约已满
                return new Result(false,MessageConstant.ORDER_FULL);
            }
        }else {
            //当前日期没有可预约
            return new Result(false,MessageConstant.SELECTED_DATE_CANNOT_ORDER);
        }
    }
    /*
     * 根据orderid，查询预约信息
     * */
    @Override
    public Map findById(Integer id) throws Exception {
        Map map = orderDao_lzl.findById(id);
        Date orderDate = (Date) map.get("orderDate");
        String s = DateUtils.parseDate2String(orderDate);
        map.put("orderDate",s);
        return map;
    }

    /*
     * 分页条件查询
     * */
    @Override
    public PageResult findPage(Map queryStrings,Member member) throws Exception {
        Integer currentPage = (Integer) queryStrings.get("currentPage");
        Integer pageSize = (Integer) queryStrings.get("pageSize");
        List queryString = (List) queryStrings.get("queryString");
        Map<String, Object> map = new HashMap<>();
        if (queryString != null && queryString.size() > 0) {
            String o = (String) queryString.get(0);
            String o1 = (String) queryString.get(1);
            Date date = DateUtils.parseString2Date(o);
            Date date1 = DateUtils.parseString2Date(o1);
            String start = new SimpleDateFormat("yyyy-MM-dd").format(date);
            String end = new SimpleDateFormat("yyyy-MM-dd").format(date1);
            map.put("start",start);
            map.put("end",end);
        }
        Integer memberId = member.getId();
        map.put("memberId",memberId);
        PageHelper.startPage(currentPage,pageSize);

        Page<Map> addressPage = orderDao_lzl.findByCondition(map);
        return new PageResult(addressPage.getTotal(),addressPage.getResult());
    }
}
